<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老人姓名" prop="code">
        <el-input
            v-model="queryParams.elderName"
            placeholder="请输入老人姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老人身份证号" prop="code">
        <el-input
            v-model="queryParams.elderIdCard"
            placeholder="请输入老人身份证号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入住时间" prop="checkinTime">
        <el-date-picker clearable
          v-model="queryParams.checkinTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择入住时间">
        </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:checkin:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:checkin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="序号" align="center">
        <template #default="scope">
          <el-tag>{{ scope.$index + 1 }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="单据编号" align="center" prop="code" />
      <el-table-column label="老人姓名" align="center" prop="elderName" />
      <el-table-column label="老人身份证号" align="center" prop="elderIdCard" />
      <el-table-column label="床位号" align="center" prop="bedNo" />

      <el-table-column label="入住时间" align="center" prop="checkinTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.checkinTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="applicant" />

      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleView(scope.row)" v-hasPermi="['system:checkin:edit']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加对话框 -->
<!--    <el-dialog-->
<!--        :title="title"-->
<!--        v-model="open"-->
<!--        width="600px"-->
<!--        append-to-body-->
<!--    >-->
<!--      <el-form-->
<!--          ref="checkinRef"-->
<!--          :model="form"-->
<!--          :rules="!isView ? rules : {}"-->
<!--          label-width="120px"-->
<!--      >-->
<!--        &lt;!&ndash; 老人信息 &ndash;&gt;-->
<!--        <el-divider>老人信息</el-divider>-->
<!--        <el-form-item label="老人姓名" prop="elderName">-->
<!--          <el-input v-model="form.elder.elderName" :disabled="isView" placeholder="请输入老人姓名" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="身份证号" prop="elderIdCard">-->
<!--          <el-input v-model="form.elder.elderIdCard" :disabled="isView" placeholder="请输入身份证号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="床位号" prop="bedNo">-->
<!--          <el-input v-model="form.elder.bedNo" :disabled="isView" placeholder="请输入床位号" />-->
<!--        </el-form-item>-->

<!--        &lt;!&ndash; 入住信息 &ndash;&gt;-->
<!--        <el-divider>入住信息</el-divider>-->
<!--        <el-form-item label="养老顾问" prop="consultant">-->
<!--          <el-input v-model="form.consultant" :disabled="isView" placeholder="请输入养老顾问" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="入住时间" prop="checkinTime">-->
<!--          <el-date-picker-->
<!--              v-model="form.checkinTime"-->
<!--              type="date"-->
<!--              value-format="YYYY-MM-DD"-->
<!--              :disabled="isView"-->
<!--              placeholder="请选择入住时间"-->
<!--          />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="入住原因" prop="reason">-->
<!--          <el-input-->
<!--              v-model="form.reason"-->
<!--              :disabled="isView"-->
<!--              type="textarea"-->
<!--              placeholder="请输入入住原因"-->
<!--          />-->
<!--        </el-form-item>-->
<!--&lt;!&ndash;        <el-form-item label="申请人" prop="applicant">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-input v-model="form.applicant" :disabled="isView" placeholder="请输入申请人" />&ndash;&gt;-->
<!--&lt;!&ndash;        </el-form-item>&ndash;&gt;-->

<!--        &lt;!&ndash; 评估信息 &ndash;&gt;-->
<!--        <el-divider>评估与其他信息</el-divider>-->
<!--        <el-form-item label="评估信息" prop="assessment">-->
<!--          <el-input-->
<!--              v-model="form.assessment"-->
<!--              :disabled="isView"-->
<!--              type="textarea"-->
<!--              placeholder="请输入评估信息"-->
<!--          />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="其他申请信息" prop="otherInfo">-->
<!--          <el-input-->
<!--              v-model="form.otherInfo"-->
<!--              :disabled="isView"-->
<!--              type="textarea"-->
<!--              placeholder="请输入其他申请信息"-->
<!--          />-->
<!--        </el-form-item>-->
<!--      </el-form>-->

<!--      &lt;!&ndash; 底部按钮 &ndash;&gt;-->
<!--      <template #footer>-->
<!--        <div class="dialog-footer">-->
<!--          <template v-if="!isView">-->
<!--            <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--          </template>-->
<!--          <el-button @click="cancel">{{ isView ? '关 闭' : '取 消' }}</el-button>-->
<!--        </div>-->
<!--      </template>-->
<!--    </el-dialog>-->
<!--   查看对话框 -->
    <!-- 查看入住办理对话框（只读） -->
    <el-dialog
        :title="title"
        v-model="look"
        width="600px"
        append-to-body
    >
      <el-form
          ref="checkinRef"
          :model="form"
          label-width="120px"
      >
        <!-- 老人信息 -->
        <el-divider>老人信息</el-divider>
        <el-form-item label="老人姓名">
          <el-input v-model="form.elderName" disabled />
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="form.elderIdCard" disabled />
        </el-form-item>
        <el-form-item label="床位号">
          <el-input v-model="form.bedNo" disabled />
        </el-form-item>

        <!-- 入住信息 -->
        <el-divider>入住信息</el-divider>
        <el-form-item label="养老顾问">
          <el-input v-model="form.consultant" disabled />
        </el-form-item>
        <el-form-item label="入住时间">
          <el-date-picker
              v-model="form.checkinTime"
              type="date"
              value-format="YYYY-MM-DD"
              disabled
          />
        </el-form-item>
        <el-form-item label="入住原因">
          <el-input
              v-model="form.reason"
              type="textarea"
              disabled
          />
        </el-form-item>
        <el-form-item label="申请人">
          <el-input v-model="form.applicant" disabled />
        </el-form-item>

        <!-- 评估信息 -->
        <el-divider>评估与其他信息</el-divider>
        <el-form-item label="评估信息">
          <el-input
              v-model="form.assessment"
              type="textarea"
              disabled
          />
        </el-form-item>
        <el-form-item label="其他申请信息">
          <el-input
              v-model="form.otherInfo"
              type="textarea"
              disabled
          />
        </el-form-item>
      </el-form>

      <!-- 底部按钮 -->
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="look = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Checkin">
import { listCheckin, getCheckin, delCheckin, addCheckin, updateCheckin } from "@/api/check/checkin.js"

const { proxy } = getCurrentInstance()

const checkinList = ref([])
const open = ref(false)
const look = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    elderName: null,
    code: null,
    checkinTime: null,
    elderIdCard: null
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询入住办理列表 */
function getList() {
  loading.value = true
  listCheckin(queryParams.value).then(response => {
    console.log(response)
    checkinList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    code: null,
    title: null,
    elderId: null,
    businessId: null,
    consultant: null,
    checkinTime: null,
    reason: null,
    applicant: null,
    departmentCode: null,
    applicantId: null,
    processStatus: null,
    approvalStatus: null,
    otherInfo: null,
    assessment: null,
    createdAt: null,
    updatedAt: null,
    remark: null,
    extend1: null,
    extend2: null
  }
  proxy.resetForm("checkinRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加入住办理"
}

/** 查看按钮操作 */
function handleView(row) {
  reset()
  const _id = row.id
  getCheckin(_id).then(response => {
    form.value = response.data
    look.value = true
    title.value = "查看入住办理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["checkinRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCheckin(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCheckin(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除入住办理编号为"' + _ids + '"的数据项？').then(function() {
    return delCheckin(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/checkin/export', {
    ...queryParams.value
  }, `checkin_${new Date().getTime()}.xlsx`)
}

getList()
</script>
